package com.hd.trans.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.MutableLiveData;

import com.hd.trans.R;


/**
 * 通用搜索框
 */
public class CommonSearchView extends FrameLayout {

    private ConstraintLayout mContainer;
    private EditText mEditText;
    private ImageView mClearBtn;

    private int mTextColor;
    private int mTextHintColor;
    private float mTextSize;

    public MutableLiveData<String> mTextChangeObserver = new MutableLiveData<>();
    public MutableLiveData<String> mBeforeTextChangeObserver = new MutableLiveData<>();

    public CommonSearchView(@NonNull Context context) {
        this(context, null);
    }

    public CommonSearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonSearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflaterView(attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContainer = findViewById(R.id.search_container);
        mEditText = findViewById(R.id.search_et);
        mClearBtn = findViewById(R.id.search_btn_clear);

        mContainer.setBackground(getBackground());

        mEditText.setTextColor(mTextColor);
        mEditText.setHintTextColor(mTextHintColor);
        mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);

        mClearBtn.setOnClickListener(view -> {
//            SoftInputUtil.hideSoftInput(view);
            mEditText.setText("");
        });
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mBeforeTextChangeObserver.setValue(charSequence.toString());
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mClearBtn.setVisibility(TextUtils.isEmpty(editable) ? View.INVISIBLE : View.VISIBLE);
                mTextChangeObserver.setValue(editable.toString());
            }
        });
    }

    private void inflaterView(AttributeSet attrs) {
        View.inflate(getContext(), R.layout.layout_search, this);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CommonSearchView);
        int defaultTextColor = ContextCompat.getColor(getContext(), R.color.search_text);
        mTextColor = typedArray.getResourceId(R.styleable.CommonSearchView_text_color, defaultTextColor);
        int defaultTextHintColor = ContextCompat.getColor(getContext(), R.color.uninput_gary);
        mTextHintColor = typedArray.getResourceId(R.styleable.CommonSearchView_text_hint_color, defaultTextHintColor);
        float defaultTextSize = getResources().getDimension(R.dimen.sp_14);
        mTextSize = typedArray.getDimension(R.styleable.CommonSearchView_text_size, defaultTextSize);
        typedArray.recycle();
    }

    public MutableLiveData<String> getTextChangeObserver() {
        return mTextChangeObserver;
    }

    public MutableLiveData<String> getBeforeTextChangeObserver() {
        return mBeforeTextChangeObserver;
    }

    public void setTextColor(@ColorInt int textColor) {
        this.mTextColor = textColor;
        mEditText.setTextColor(mTextColor);
    }

    public void setTextHintColor(@ColorInt int textHintColor) {
        this.mTextHintColor = textHintColor;
        mEditText.setHintTextColor(mTextHintColor);
    }

    public void setTextSize(float textSize) {
        this.mTextSize = textSize;
        mEditText.setTextSize(mTextSize);
    }

    @Override
    public void setBackgroundResource(@DrawableRes int resid) {
        super.setBackgroundResource(resid);
    }

    @Override
    public void setBackground(Drawable background) {
        super.setBackground(background);
    }

    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
    }
}
